﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WebApplication1
{
    public partial class Prebaruvanje : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SlikaPocetna.ImageUrl = "Sliki/RCZ-Design[320x240].jpg";
            LinkButton1.Visible = false;
        }

        protected void PrebaruvanjeListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Model.Text = PrebaruvanjeListBox.SelectedValue;
            ModelLabel.Text = Proizvoditel.Text + " " + Model.Text;
            LoadInfoModel(PrebaruvanjeListBox.SelectedValue);
        }

        private void PrebaruvajModel(String Input)
        {
            
                PrebaruvanjeListBox.Items.Clear();
                MarkaLabel.Text = ModelLabel1.Text = TipLabel.Text = ModelLabel.Text = ModelHyperLink.Text = "";
                LinkButton1.Visible = false;
                
                if (Input != "")
                {
                    Input = "%" + Input + "%";

                    MySqlConnection konekcija = new MySqlConnection();
                    konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
                    // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

                    MySqlCommand komanda = new MySqlCommand();
                    komanda.Connection = konekcija;
                    komanda.CommandText = "SELECT * FROM modeli WHERE (MarkaSifra LIKE '" + Input + "' OR Proizvoditel LIKE '" + Input + "' OR Model LIKE '" + Input + "')";

                    try
                    {
                        konekcija.Open();

                        MySqlDataReader citac = komanda.ExecuteReader();
                        while (citac.Read())
                        {
                            ListItem element = new ListItem();
                            element.Text = citac["Proizvoditel"].ToString() + " " + citac["Model"].ToString();
                            element.Value = citac["Model"].ToString();
                            PrebaruvanjeListBox.Items.Add(element);
                        }
                        citac.Close();
                    }
                    catch (Exception ex)
                    {
                        //   Label2.Text = ex.Message;
                    }
                    finally
                    {
                        konekcija.Close();
                    }
                }
                else
                {
                    SlikaPocetna.ImageUrl = "Sliki/RCZ-Design[320x240].jpg";
                    ModelLabel.Text = MarkaLabel.Text = ModelLabel1.Text = TipLabel.Text = ModelHyperLink.Text = ModelHyperLink.NavigateUrl = ModelHyperLink.ToolTip = ModelHyperLink.Target = LinkButton1.ToolTip = "";
                    LinkButton1.Visible = false;
                }
        }

        protected void LoadInfoModel(string ID)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT * FROM modeli M, proizvoditeli P WHERE M.Model='" + ID + "' AND M.Proizvoditel=P.ImeProizvoditel";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                if (citac.Read())
                {
                    MarkaLabel.Text = citac["Proizvoditel"].ToString();
                    ModelLabel1.Text = citac["Model"].ToString();
                    TipLabel.Text = citac["Tip"].ToString();
                    Proizvoditel.Text = citac["Proizvoditel"].ToString();
                    ModelLabel.Text = MarkaLabel.Text + " " + ModelLabel1.Text;

                    ModelHyperLink.Text = citac["WebProizvoditeli"].ToString();
                    ModelHyperLink.NavigateUrl = citac["WebModeli"].ToString();
                    ModelHyperLink.ToolTip = "Официјална web страна за " + Proizvoditel.Text + " " + Model.Text;
                    ModelHyperLink.Target = "_blank";
                    LinkButton1.Visible = true;
                    LinkButton1.ToolTip = "Повеќе информации за " + Proizvoditel.Text + " " + Model.Text;
                    SlikaPocetna.ImageUrl = "Sliki/" + MarkaLabel.Text + "/320x240/" + citac["Slika"].ToString();
                    citac.Close();
                }
            }
            catch (Exception ex)
            {
                // Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Informacii.aspx?Marka=" + Proizvoditel.Text + "&Model=" + Model.Text);
        }

        protected void PrebaruvanjeButton_Click(object sender, EventArgs e)
        {
            PrebaruvajModel(PrebaruvanjeTextBox.Text);
        }

    }
}
